﻿#region License
/***********************************************
 *  SearchEF v.1
 *  Written by James Levingston
 *  
 *  To get latest version or to see latest info:
 *  http://searchef.codeplex.com/
 *  
 *  The MIT License (MIT)
 *  http://en.wikipedia.org/wiki/MIT_License
 *  Copyright (C) <2011> by <James Levingston>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.

 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 ***/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace SearchEF
{
    #region Generic
    /// <summary>
    /// Interface type that defines an instance of work using the ObjectContext - T
    /// </summary>
    /// <typeparam name="T">Object Context</typeparam>
    public interface IUnitOfWork<T> : IDisposable
    {
        T ObjectContext { get; }
        void SetContext();
    }
    /// <summary>
    /// An instance of work using the ObjectContext - T
    /// </summary>
    /// <typeparam name="T">Object Context</typeparam>
    public class UnitOfWork<T> : IDisposable, IUnitOfWork<T> where T : ObjectContext, new()
    {
        /// <summary>
        /// Returns a ObjectContext object of type T 
        /// </summary>
        public T ObjectContext { get; private set; }

        /// <summary>
        /// When invoked, creates a new instance of T
        /// </summary>
        public UnitOfWork()
        {
            this.SetContext();
        }
        /// <summary>
        /// Set the object Context - this method can be overridden
        /// </summary>
        public virtual void SetContext()
        {
            this.ObjectContext = Activator.CreateInstance(typeof(T)) as T;
        }
        /// <summary>
        /// Passes in new instance of T
        /// </summary>
        /// <param name="context"></param>
        public UnitOfWork(T context)
        {
            if (context == null)
                throw new ArgumentNullException("Please supply an object context");

            this.ObjectContext = context;
        }
        /// <summary>
        /// IDisposable implementor
        /// </summary>
        public virtual void Dispose()
        {
            this.ObjectContext.Dispose();
        }
    }
    #endregion

    #region Non-Generic
    /// <summary>
    /// Interface type that defines an instance of work using the ObjectContext
    /// </summary>
    public interface IUnitOfWork : IDisposable
    {
        ObjectContext ObjectContext { get; }
    }
    /// <summary>
    /// A instance of work using the ObjectContext
    /// </summary>
    public class UnitOfWork : IDisposable, IUnitOfWork
    {
        /// <summary>
        /// Returns a ObjectContext object of type T 
        /// </summary>
        public ObjectContext ObjectContext { get; private set; }

        /// <summary>
        /// Passes in new instance of T
        /// </summary>
        /// <param name="context"></param>
        public UnitOfWork(object context)
        {
            if (context == null)
                throw new ArgumentNullException("Please supply an object context");

            if(context is ObjectContext)
                this.ObjectContext = (ObjectContext)context;
        }
        /// <summary>
        /// IDisposable implementor
        /// </summary>
        public virtual void Dispose()
        {
            this.ObjectContext.Dispose();
        }
    }
    #endregion
}
